/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.screens;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
 * All GUI screens implement this for common screen functions (nifty).
 * @author Krummer
 */
public class ScreenAppController extends AbstractAppState implements ScreenController {
    
    private SimpleApplication app;
    private AppStateManager stateManager;
    private Node localGuiNode;
    private Node localRootNode;
    
    //Nifty GUI stuff
    private Nifty nifty;
    private Screen screen;

    /* APP STATE FUNCTIONS */
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
       super.initialize(stateManager, app);
       this.stateManager = stateManager;
       this.app = (SimpleApplication) app;
       
       //create local container nodes
       this.localRootNode=new Node(this.getClass().toString() + "Screen");
       this.localGuiNode=new Node(this.getClass().toString() + "StartGUI"); 
    }
    
    @Override
    public void setEnabled(boolean enabled) {
        if(enabled) {
            this.app.getRootNode().attachChild(this.localRootNode);
            this.app.getGuiNode().attachChild(this.localGuiNode);
        } else {
            this.app.getRootNode().detachChild(this.localRootNode);
            this.app.getGuiNode().detachChild(this.localGuiNode);
        }
    }
    
    /* NIFTY FUNCTIONS */
    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        this.screen = screen;
    }

    public void onStartScreen() {
        this.setEnabled(true);
        this.app.getStateManager().attach(this);
    }

    public void onEndScreen() {
        this.app.getStateManager().detach(this);
    }
    
    /* GETTER SETTER */
    
    public Nifty getNifty() {
        return this.nifty;
    }
    
    public SimpleApplication getApp() {
        return this.app;
    }
    
    public Node getLocalRootNode() {
        return this.localRootNode;
    }
    
}
